package com.lul15834.orderfood.activity.user.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.ManagerUpdateFood;
import com.lul15834.orderfood.activity.user.ManageUser;
import com.lul15834.orderfood.activity.user.UserBuyActivity;
import com.lul15834.orderfood.bean.FoodBean;
import com.lul15834.orderfood.bean.UserBean;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.dao.CommentDao;
import com.lul15834.orderfood.dao.FoodDao;

import java.util.List;

/**
 * 显示商家商品的adapter
 */
public class FoodListUserAdapter extends ArrayAdapter<FoodBean> {
    private List<FoodBean> list;
    private Context context;
    public FoodListUserAdapter(@NonNull Context context, List<FoodBean> list){
        super(context, R.layout.list_user_food_list,list);
        this.context=context;
        this.list=list;
    }
    @Override
    public View getView(int position,View convertView, ViewGroup viewGroup){
        // position数据的位置，convertView可重用的视图对象，viewGroup父容器，测量和布局子视图
        if (convertView==null){
            LayoutInflater inflater=LayoutInflater.from(getContext());
            convertView=inflater.inflate(R.layout.list_user_food_list,viewGroup,false);
        }
        FoodBean tem=list.get(position);
        ImageView img=convertView.findViewById(R.id.user_food_list_foodImg);
        TextView name=convertView.findViewById(R.id.user_food_list_name);
        TextView num=convertView.findViewById(R.id.user_food_list_saleNum);//不在数据库里
        TextView price=convertView.findViewById(R.id.user_food_list_price);
        TextView des=convertView.findViewById(R.id.user_food_list_des);
        ImageView imgB=convertView.findViewById(R.id.user_food_list_businessImg);
        TextView nameB=convertView.findViewById(R.id.user_food_list_businessName);
        TextView fen=convertView.findViewById(R.id.user_food_list_businessFen);
        String businessId=tem.getBusinessId();//商家的账号
        UserBean businessUser=AdminDao.getBusinessUser(businessId);
        imgB.setImageBitmap(BitmapFactory.decodeFile(businessUser.getsImg()));
        nameB.setText(businessUser.getsName());
        //评分计算 获取所有订单分数
        String fenAvg=CommentDao.getAvgScoreBusiness(businessId);
        fen.setText(fenAvg+" 分");
        //月售统计当前月份的销售
        Bitmap bitmap= BitmapFactory.decodeFile(tem.getFoodImage());
        img.setImageBitmap(bitmap);
        name.setText(tem.getFoodName());
        price.setText("价格"+tem.getFoodPrice());
        des.setText("描述"+tem.getFoodDescription());
        int saleNum=FoodDao.getMouSaleNum(tem.getFoodId());
        num.setText("月销:"+String.valueOf(saleNum));
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), UserBuyActivity.class);
                intent.putExtra("business",businessUser);
                getContext().startActivity(intent);
            }
        });
        return convertView;
    }
}

